﻿using System;
/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：徐彦龙 
    //
    // 文件名：RoleAdd.cs
    // 文件功能描述：角色管理，新增角色
    // 创建标识：徐彦龙2009-03-06
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System.Web.UI.WebControls;
using System.Collections.Generic;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using System.Data;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class RoleAdd : BasePage
    {
        public RoleAdd()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.主用户管理.ToString();
        }

        Role role = new Role();
        protected void Page_Load(object sender, EventArgs e)
        {
            //非超级管理员不允许操作此页面
            if (!CurrentAccount.IsSuperAdmin())
            {
                PageError("非超级管理员不允许操作此功能","");
            }

            if (!this.IsPostBack)
            {
               // this.SetBindToRule();
            }
            
        }


        ///// <summary>
        ///// 显示所有权限给新增用户
        ///// </summary>
        //private void SetBindToRule()
        //{
        //    IList<OperatingRule> lr = new List<OperatingRule>();
        //    IBLL_OperatingRule ior = Factory.BLL_OperatingRule();
        //    try
        //    {
        //        lr = ior.GetAllOperatingRule();
        //        this.gvwRoleRule.DataSource = lr;
        //        this.gvwRoleRule.DataBind();
        //    }
        //    catch (Exception ex)
        //    {
        //        PageError(ex.Message.ToString(),"RoleList.aspx");
        //    }
        //}
       
        /// <summary>
        /// 保存角色表、角色权限表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //调用方法实现插入“角色表”、“角色权限表”
                role.RoleName = this.txtRoleName.Text;
                IBLL_Role ior = Factory.BLL_Role();
                List<OperatingRule> operatingRuleList = new List<OperatingRule>();

                string ids = Request["checkbox1"];
                if (!string.IsNullOrEmpty(ids))
                {
                    string[] ArrayIDSs = ids.Split(',');

                    foreach (string item in ArrayIDSs)
                    {
                        OperatingRule operatingRule = new OperatingRule();
                        operatingRule.OperatingRuleID = item;
                        operatingRuleList.Add(operatingRule);

                    }
                }
                role.OperatingRuleList = operatingRuleList;

                int flag = ior.AddRole(role);
                if (flag == 1)
                {
                    PageInfo("角色添加成功！", this.ReturnUrl);
                }
                if (flag == 2)
                {
                    PageError("该角色名称已经存在！", this.ReturnUrl);
                }
                if (flag == 0)
                {
                    PageError("角色添加失败！", this.ReturnUrl);
                }
            }
        }

        protected void txtRoleID_TextChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 把机构权限的“true”、“false”修改成“是”、“否”汉字显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwRoleRule_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    Label lblQiye = e.Row.FindControl("lblQiye") as Label;
                    Label lblZX = e.Row.FindControl("lblZX") as Label;
                    Label lblYY = e.Row.FindControl("lblYY") as Label; 
                    if(lblQiye != null)
                    {
                        if (lblQiye.Text == "True")
                        {
                            lblQiye.Text = "是";
                        }
                        else
                        {
                            lblQiye.Text = "否";
                        }
                    }
                    if (lblZX != null)
                    {
                        if (lblZX.Text == "True")
                        {
                            lblZX.Text = "是";
                        }
                        else
                        {
                            lblZX.Text = "否";
                        }
                    }
                    if (lblYY != null)
                    {
                        if (lblYY.Text == "True")
                        {
                            lblYY.Text = "是";
                        }
                        else
                        {
                            lblYY.Text = "否";
                        }
                    }
                }
            }
        }

        protected void gvwRoleRule_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 双向排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        protected void gvwRoleRule_Sorting(object sender, GridViewSortEventArgs e)
        {
            //string sPage = e.SortExpression;
            //if (ViewState["SortOrder"].ToString() == sPage)
            //{
            //    if (ViewState["OrderDire"].ToString() == "Desc")
            //        ViewState["OrderDire"] = "ASC";
            //    else
            //        ViewState["OrderDire"] = "Desc";
            //}
            //else
            //{
            //    ViewState["SortOrder"] = e.SortExpression;
            //}
            //this.SetBindToRule();
            
        }

    }
}

